<?php

namespace App\Jobs\Pay;

use App\Models\Trip\OrderPay\Repositories\Interfaces\OrderPayRepositoryInterface;
use App\Models\Trip\RefundNeedPay\RefundNeedPay;
use App\Models\Trip\SnapTaskUpgradePay\SnapTaskUpgradePay;
use App\Trip\Payment\PaymentAttach;
use App\Trip\Payment\PaymentBusiness;
use App\Trip\Wechat\Pay\PayRes\PayResult;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;

class ProcessWechatOrderPayCallback implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public bool $deleteWhenMissingModels = true;
    /**
     * 作业的唯一锁将被释放的秒数。
     *
     * @var int
     */
    public int        $uniqueFor = 60;
    private PayResult $payResult;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(PayResult $payResult)
    {
        $this->payResult = $payResult;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws Exception
     * @throws Throwable
     */
    public function handle()
    {

        $pay_sn         = $this->payResult->getOutTradeNo();
        $attach         = $this->payResult->getAttach();
        $payment_attach = PaymentBusiness::parsePaymentAttach($attach);
        Log::info("支付回调:{$pay_sn},type:{$payment_attach->getType()}");
        switch ($payment_attach->getType()) {
            case PaymentAttach::PAY_TYPE_ORDER:
                // 微信支付下单回调
                $orderPayRepo = app(OrderPayRepositoryInterface::class);
                $orderPayRepo->updatePayByPayResult($pay_sn, $this->payResult);
                break;
            case PaymentAttach::PAY_TYPE_APPLY_REFUND:
                // 退款补差额支付回调
                $need_pay = new RefundNeedPay();
                $need_pay->updatePayByPayResult($pay_sn, $this->payResult);
                break;
            case PaymentAttach::PAY_TYPE_SNAP_TASK_UPGRADE:
                // 升级高速付费抢票
                $pay = new SnapTaskUpgradePay();
                $pay->updatePayByPayResult($pay_sn, $this->payResult);
                break;
            case PaymentAttach::PAY_TYPE_SNAP_TASK_EDIT:
                // 编辑抢票任务 需支付差价
                $orderPayRepo = app(OrderPayRepositoryInterface::class);
                $orderPayRepo->updatePayByPayResult($pay_sn, $this->payResult);
                break;
            default:
        }
        Log::info("支付回调:{$pay_sn},type:{$payment_attach->getType()},业务处理成功");
    }

    /**
     * 任务的唯一 ID。
     *
     * @return string
     */
    public function uniqueId(): string
    {
        return $this->payResult->getOutTradeNo();
    }

    public function failed()
    {
        $pay_sn = $this->payResult->getOutTradeNo();
        Log::error("支付单号回调业务处理失败：{$pay_sn}");
    }


    /**
     * 计算重试任务之前要等待的秒数。
     *
     * @return array
     */
    public function backoff()
    {
        return [5, 30, 60, 120, 600, 1800, 3600];
    }
}
